package com.AX.controller;

import com.AX.entity.User;
import com.AX.exception.NoPathException;
import com.AX.service.UserDetailsService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    @Resource
    private final UserDetailsService userDetailsService;

    @PostMapping("/login")
    public ResponseEntity<User> login(@RequestParam String username, @RequestParam String passward) {
        System.out.println("1");
        System.out.println(username);
        System.out.println(passward);
        try {
            return ResponseEntity.ok(userDetailsService.yz(username,passward));
        } catch (NoPathException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getPassward());
        User registeredUser = null;
        try {
            registeredUser = userDetailsService.register(user);
        } catch (NoPathException e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.ok(registeredUser);
    }


}

